package com.springboot;

import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.Point;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;
import org.influxdb.impl.InfluxDBResultMapper;

import java.util.List;

public class InfluxDB_Demo {
    public static void main(String[] args){
        insert();
        query2();
    }

    /**
     * 插入
     */
    public static void insert(){
        InfluxDB influxDB=InfluxDBFactory.connect("http://localhost:8086","root","root");
        influxDB.setDatabase("mydb");//设置连接的"数据库"
        Point point=Point.measurement("person")//设置连接的"表"
                .addField("name","cf")//这里是添加的字段及其值
                .addField("age",26)
                .addField("address","wh")
                .build();
        influxDB.write(point);//写入
    }

    /**
     * 返回的Result处理起来挺麻烦的，不推荐这种方式
     */
    public void query(){
        InfluxDB influxDB=InfluxDBFactory.connect("http://localhost:8086","root","root");
        Query query=new Query("SELCT * FROM person WHERE time > now() - 5h","mydb");
        QueryResult result=influxDB.query(query);

    }

    /**
     * 这种方式直接转为对象
     */
    public static void query2(){
        InfluxDB influxDB=InfluxDBFactory.connect("http://localhost:8086","root","root");
        String dbName = "mydb";
        QueryResult queryResult = influxDB.query(new Query("SELECT * FROM person", dbName));

        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper();
        List<Person> list = resultMapper.toPOJO(queryResult, Person.class);
        System.out.println(list);
    }
}
